package com.bwda.dsrs.filecenter.util;

import org.apache.commons.lang.StringUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author youzhen
 * @date 2019/4/9
 */
public class DesensUtil {

    /**
     * 脱敏匹配
     */
    public static List<String> matching(String keywordValue, String testContent) throws Exception {
        Pattern pattern = Pattern.compile(keywordValue);
        List<String> testList = getContentByList(testContent);
        Matcher matcher = null;
        List<String> matchResultList = new ArrayList<String>();
        for (String strText : testList) {
            matcher = pattern.matcher(strText);
            while (matcher.find()) {
                matchResultList.add(matcher.group());
            }
        }
        return matchResultList;
    }

    /**
     * 脱敏匹配
     */
    private static List<String> getContentByList(String testContent) throws Exception {
        String[] testArr = testContent.split(",");
        List<String> testList = Arrays.asList(testArr);
        return testList;
    }

    /**
     * 脱敏分组
     */
    public static List<List<String>> grouping(String replaceRegex, String matchResult)
            throws Exception {
        List<String> testList = getContentByList(matchResult);
        List<List<String>> groupList = new ArrayList<List<String>>();
        List<String> group = null;
        for (String strText : testList) {
            group = new ArrayList<String>();
            group = startGrouping(strText, replaceRegex);
            groupList.add(group);
        }
        return groupList;
    }

    /**
     * 脱敏分组
     */
    private static List<String> startGrouping(String strText, String replaceRegex) throws Exception {
        Pattern pattern = Pattern.compile(replaceRegex);
        Matcher matcherReplace = null;
        matcherReplace = pattern.matcher(strText);
        List<String> groupList = new ArrayList<String>();
        while (matcherReplace.find()) {
            int intGroupCount = matcherReplace.groupCount();
            for (int i = 1; i <= intGroupCount; i++) {
                groupList.add(matcherReplace.group(i));
            }
        }
        return groupList;
    }

    /**
     * 替换分组内容
     */
    public static List<String> validating(String testContent, String group, String keywordValue, String replaceRegex) throws Exception {
        List<String> testResult = new ArrayList<String>();
        List<String> testList = getContentByList(testContent);
        if (StringUtils.isNotBlank(group)) {
            List<Integer> groupList = new ArrayList<Integer>();
            String[] groupArr = group.split(",");
            for (int i = 0; i < groupArr.length; i++) {
                groupList.add(Integer.parseInt(groupArr[i]
                        .substring(groupArr[i].indexOf('_') + 1)));
            }
            Pattern pattern = Pattern.compile(keywordValue);
            Matcher matcher = null;
            StringBuffer strBuffText = new StringBuffer();
            for (String strText : testList) {
                strBuffText.append(strText);
                matcher = pattern.matcher(strText);
                while (matcher.find()) {
                    replace(strBuffText, matcher, replaceRegex, groupList);
                }
                testResult.add(strBuffText.toString());
                strBuffText.setLength(0);
            }
        } else {
            String[] strArrayContent = StringUtils.split(testContent, ",");
            if (strArrayContent != null) {
                for (String strContent : strArrayContent) {
                    List<String> matchList = matching(keywordValue, testContent);
                    boolean boolMatched = false;
                    for (String str : matchList) {
                        if (StringUtils.equals(strContent, str)) {
                            boolMatched = true;
                            break;
                        }
                    }
                    if (boolMatched == true) {
                        testResult.add(StringUtils.replace(strContent, replaceRegex, "*"));
                    } else {
                        testResult.add(strContent);
                    }
                }
            }
        }
        return testResult;
    }

    /**
     * 替换分组内容
     */
    public static void replace(StringBuffer strBuffText, Matcher matcher,
                               String replaceRegex, List<Integer> groupList) {
        Pattern pattern = Pattern.compile(replaceRegex);
        Matcher matcherReplace = null;
        matcherReplace = pattern.matcher(matcher.group(0));
        int intStart = matcher.start(0);
        while (matcherReplace.find()) {
            int intGroupCount = matcherReplace.groupCount();
            for (Integer inteGroup : groupList) {
                if (inteGroup <= intGroupCount) {
                    if (StringUtils.isNotBlank(matcherReplace.group(inteGroup))) {
                        int startIndex = matcherReplace.start(inteGroup);
                        int endIndex = matcherReplace.end(inteGroup);
                        strBuffText.replace(intStart + startIndex, intStart
                                + endIndex, linkReplaceStr(startIndex, endIndex, "*"));
                    }
                }
            }
        }
    }

    /**
     * 替换分组内容
     */
    public static String linkReplaceStr(int intStart, int intEnd, String replaceStr) {
        StringBuilder strResult = new StringBuilder();
        for (; intStart < intEnd; intStart++) {
            strResult.append(replaceStr);
        }
        return strResult.toString();
    }

}
